package cn.bdqn.controller;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import cn.bdqn.entity.Complaints;
import cn.bdqn.entity.Repair;
import cn.bdqn.entity.Users;
import cn.bdqn.entity.UsersAndComVo;
import cn.bdqn.service.ComplaintsService;
import cn.bdqn.util.Page;


@Controller
@RequestMapping("complaints")
public class ComplaintsController {
	@Autowired
	private ComplaintsService service;
	
	@RequestMapping("saveComplaints")
	public String saveComplaints(Integer _userid,HttpSession session, HttpServletRequest request,Complaints complaints,Model model,@RequestParam(value="a_idPicPath1",required=false) MultipartFile attach) {
		String postimg=null;
		complaints.set_userid(_userid);
		//判断文件是否为空
		if(!attach.isEmpty()) {
			//定义文件上传路径
			String path=request.getSession().getServletContext().getRealPath("statics"+File.separator+"images");
			String oldFileName=attach.getOriginalFilename();
			String prefix=FilenameUtils.getExtension(oldFileName);
			int filesize=5000000;
			if(attach.getSize()>filesize) {
				request.setAttribute("message", "* 文件大小不得超过500k");
				return "main";
			}else if(prefix.equalsIgnoreCase("jpg")||prefix.equalsIgnoreCase("jpeg")||prefix.equalsIgnoreCase("png")||prefix.equalsIgnoreCase("pneg")) {
				//获取当前系统时间和随机数+"_Personal.jpg"
				String fileName=System.currentTimeMillis()+RandomUtils.nextInt(1000000)+"_Personal.jpg";
				File targetFile=new File(path, fileName);
				if(!targetFile.exists()) {
					targetFile.mkdirs();
				}
				try {
					attach.transferTo(targetFile);
				} catch (Exception e) {
					e.printStackTrace();
					request.setAttribute("message", "* 上传失败");
					return "main";
				}
				postimg="/dormitory/statics/images/"+File.separator+fileName;
			}else {
				request.setAttribute("message", "* 上传图片格式必须是jpg、jpeg、png、pneg");
				return "main";
			}
		}
		complaints.setContextimg(postimg);
		Integer count=service.saveComplaints(complaints);
		System.out.println("sdsds");
		return "forward:/users/page";
	}
	//普通用户删除
	@RequestMapping("deleteComplaints")
	public String deleteComplaints(Integer complaintsid,Integer pageNo) {
		Integer num=service.deleteComplaints(complaintsid);
		System.out.println("dangqianye"+pageNo);
		return "forward:/users/toshouye?pageNo="+pageNo;
	}
	//管理删除
	@RequestMapping("guandeleteComplaints")
	public String guandeleteComplaints(Integer complaintsid,Integer pageNo) {
		Integer num=service.deleteComplaints(complaintsid);
		System.out.println("dangqianye"+pageNo);
		return "forward:/complaints/toguanComplaints?pageNo="+pageNo;
	}
	//管理吐槽
	@RequestMapping("toguanComplaints")
	public String toguanComplaints(String context,Integer pageNo,Integer pageSize,Model model) {
		if(pageNo==null) {
			pageNo=1;
		}
		if(pageSize==null) {
			pageSize=3;
		}
		if(context==null) {
			context="";
		}
		Page<UsersAndComVo> page=new Page<>();
		page.setPageSize(pageSize);
		page.setPageNo(pageNo);
		service.queryAllComplaints(context, page);
		model.addAttribute("context", context);
		model.addAttribute("page", page);
		return "guanComplaints";
	}
	
}
